/**
 * 详情对话框
 */
var BtyZxBaoxiaodanInfoDlg = {
    data: {
        id: "",
        shopName: "",
        userName: "",
        userId: "",
        productRemark: "",
        applyDate: "",
        auditTime: "",
        reason: "",
        status: "",
        fileUrl: "",
        bxdaId: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: "",
        reimbursementAmount: "",
        remitAmount: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        ,url: '/btyZxBaoxiaodan/upload1' //上传接口
        ,accept: 'file' //允许上传的文件类型
        ,data: {
            id: function(){
                return $('#id').val();
            }
        }
        ,done: function(res){
            //上传完毕回调
            $('#fileUrl').val(res.data.src);
            Feng.success("货品单导入成功！");
        }
        ,error: function(){
            //请求异常回调
        }
    });

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodan/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('btyZxBaoxiaodanForm', result.data);
    $('#reason').html(result.data.reason);
    $('#download').click(function(){
//        alert($('#fileUrl').val())
//        window.location.href = Feng.ctxPath + $('#fileUrl').val();
        downloadByUrl({
          url: Feng.ctxPath + $('#fileUrl').val(),
          target: '_self',
          fileName: "货品单",
        });
    });

    function openWindow(url, opt) {
        const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
        const feature = [];
        noopener && feature.push('noopener=yes');
        noreferrer && feature.push('noreferrer=yes');
        window.open(url, target, feature.join(','));
    }
    function downloadByUrl({
      url,
      target = '_blank',
      fileName,
    }) {
      const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
      const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;

      if (/(iP)/g.test(window.navigator.userAgent)) {
        console.error('Your browser does not support download!');
        return false;
      }
      if (isChrome || isSafari) {
        const link = document.createElement('a');
        link.href = url;
        link.target = target;

        if (link.download !== undefined) {
          link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
        }

        if (document.createEvent) {
          const e = document.createEvent('MouseEvents');
          e.initEvent('click', true, true);
          link.dispatchEvent(e);
          return true;
        }
      }
      if (url.indexOf('?') === -1) {
        url += '?download';
      }

      openWindow(url, { target });
      return true;
    }
});